package com.lsk.servicemap.service;

import com.lsk.internalcommon.DTO.DicDistrict;
import com.lsk.internalcommon.DTO.ResponseResult;
import com.lsk.internalcommon.constant.AMapConfigConstants;
import com.lsk.internalcommon.constant.CommonStatusEnum;
import com.lsk.servicemap.mapper.DicDistrictMapper;
import com.lsk.servicemap.remote.DicDistrictClient;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class DicDistrictService {

    @Value("${map.key}")
    private String mapKey;

    @Autowired
    private DicDistrictClient districtClient;

    @Autowired
    private DicDistrictMapper districtMapper;

    public ResponseResult Initdicdistrict(String keywords){

        String s = districtClient.dicdistrict(keywords);
        JSONObject jsonObject = JSONObject.fromObject(s);
        int status = jsonObject.getInt(AMapConfigConstants.STATUS);
        if (status!=1){
            return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode(),CommonStatusEnum.MAP_DISTRICT_ERROR.getValue());
        }
        JSONArray jsonArray = jsonObject.getJSONArray(AMapConfigConstants.DISTRICTS);
        for (int country=0;country<jsonArray.size();country++){
            //第一级别
            JSONObject countryjsonObject = jsonArray.getJSONObject(country);
            String AddressCode = countryjsonObject.getString(AMapConfigConstants.ADCODE);
            String AddressName =countryjsonObject.getString(AMapConfigConstants.NAME);
            String parentAddressCode = "0";
            String level = countryjsonObject.getString(AMapConfigConstants.LEVEL);
            insert(AddressCode,AddressName,parentAddressCode,level);
            JSONArray provinceJsonArray = countryjsonObject.getJSONArray(AMapConfigConstants.DISTRICTS);
            for (int p=0;p<provinceJsonArray.size();p++){
                //省份级别
                JSONObject provinceJsonObject = provinceJsonArray.getJSONObject(p);
                String provinceAddressCode = provinceJsonObject.getString(AMapConfigConstants.ADCODE);
                String provinceAddressName =provinceJsonObject.getString(AMapConfigConstants.NAME);
                String provinceParentAddressCode = AddressCode;
                String provinceLevel = provinceJsonObject.getString(AMapConfigConstants.LEVEL);
                insert(provinceAddressCode,provinceAddressName,provinceParentAddressCode,provinceLevel);
                JSONArray cityJsonArray = provinceJsonObject.getJSONArray(AMapConfigConstants.DISTRICTS);
                for (int c=0;c<cityJsonArray.size();c++){
                    //市级别
                    JSONObject cityJsonObject = cityJsonArray.getJSONObject(c);
                    String cityAddressCode = cityJsonObject.getString(AMapConfigConstants.ADCODE);
                    String cityAddressName =cityJsonObject.getString(AMapConfigConstants.NAME);
                    String cityParentAddressCode = provinceAddressCode;
                    String cityLevel = cityJsonObject.getString(AMapConfigConstants.LEVEL);
                    insert(cityAddressCode,cityAddressName,cityParentAddressCode,cityLevel);
                    JSONArray AreaJsonArray = cityJsonObject.getJSONArray(AMapConfigConstants.DISTRICTS);
                    for (int a=0;a<AreaJsonArray.size();a++){
                        //区县级别
                        JSONObject AreaJsonObject = AreaJsonArray.getJSONObject(a);
                        String AreaAddressCode = AreaJsonObject.getString(AMapConfigConstants.ADCODE);
                        String AreaAddressName =AreaJsonObject.getString(AMapConfigConstants.NAME);
                        String AreaParentAddressCode = cityAddressCode;
                        String AreaLevel = AreaJsonObject.getString(AMapConfigConstants.LEVEL);
                        insert(AreaAddressCode,AreaAddressName,AreaParentAddressCode,AreaLevel);
                    }
                }
            }

        }
        //插入到数据库
        return ResponseResult.success();
    }

    //添加到数据库表中
    private void insert(String AddressCode,String AddressName,String parentAddressCode,String level){
        int convertLevel = 0;
        DicDistrict dicDistrict = new DicDistrict();
        dicDistrict.setAddressCode(AddressCode);
        dicDistrict.setAddressName(AddressName);
        dicDistrict.setParentAddressCode(parentAddressCode);
        if (level.trim().equals("country")){
            convertLevel = 0;
        }else if (level.trim().equals("province")){
            convertLevel = 1;
        }else if (level.trim().equals("city")){
            convertLevel = 2;
        }else {
            convertLevel = 3;
        }
        dicDistrict.setLevel(convertLevel);
        districtMapper.insert(dicDistrict);
    }
}
